package com.lenway.requestpermissionsample

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.lenway.requestpermission.requestPermissions

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btn_apply1).setOnClickListener(View.OnClickListener {
            requestPermission(Manifest.permission.CAMERA);
        })

        findViewById<Button>(R.id.btn_apply2).setOnClickListener(View.OnClickListener {
            requestPermission(Manifest.permission.RECORD_AUDIO);
        })

        findViewById<Button>(R.id.btn_apply3).setOnClickListener(View.OnClickListener {
            requestPermission(Manifest.permission.READ_CONTACTS);
        })

        findViewById<Button>(R.id.btn_apply4).setOnClickListener(View.OnClickListener {
            requestPermission(Manifest.permission.WRITE_CONTACTS);
        })

        findViewById<Button>(R.id.btn_applym).setOnClickListener(View.OnClickListener {
            requestPermissionM();
        })
    }

    private fun requestPermission(permissions:String){
        requestPermissions(permissions).granted {
            //权限被同意
            Toast.makeText(this@MainActivity, "权限被同意:" + it.javaClass.simpleName, Toast.LENGTH_LONG).show()
        }.denied {
            //权限被拒绝
            Toast.makeText(
                this@MainActivity,
                "拒绝:" + it.deniedPermissions + "  永久拒绝:" + it.deniedPermissionsPermanently,
                Toast.LENGTH_LONG
            ).show()

        }
    }

    private fun requestPermissionM(){
        requestPermissions(Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
        ).granted {
            //权限被同意
            Toast.makeText(this@MainActivity, "权限被同意:" + it.javaClass.simpleName, Toast.LENGTH_LONG).show()
        }.denied {
            //权限被拒绝
            Toast.makeText(
                this@MainActivity,
                "拒绝:" + it.deniedPermissions + "  永久拒绝:" + it.deniedPermissionsPermanently,
                Toast.LENGTH_LONG
            ).show()

        }
    }
}